package app.web.cartoon.report.num.tab;

import cn.lily.com.component.widget.TElement;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.exception.YaconException;
import cn.lily.com.lang.BusinessDate;
import cn.lily.com.lang.BusinessDateEx;
import cn.lily.com.report.base.data.series.ESeries;
import cn.lily.com.report.base.data.series.ESeriesLine;
import cn.lily.com.report.base.entity.EToolBox;
import cn.lily.com.report.base.entity.EToolTip;
import cn.lily.com.report.com.line.ELineCharts;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.page.TPanel;
/**
 * 动漫总数统计
 * @author JiaJ
 * @date 2017年12月7日
 *
 */
public class NumTotalReport extends BusinessPage {

	@Override
	public void page_onLoad() throws YaconException {
		
		TPanel numTotalPanel = Panel("numTotalPanel");
		numTotalPanel.creatToolbar().setBarText("【"+BusinessDate.getToday()+"】动漫总数增量统计");
		
		//无关闭按钮
		numTotalPanel.setClose(false);
		numTotalPanel.createPanelCell(this.instCharts());
		
		getPage().addElement(numTotalPanel);
		
	}

	private TElement instCharts() {
		
		//柱形图对象
		ELineCharts lineCharts = new ELineCharts("numTotalLine");
		
		//悬浮控制
		EToolTip toolTip = lineCharts.createToolTip(EToolTip.TRIGGER_ITEM);
		//格式化方法
		toolTip.createFormatter("function (params) {"
			       +     "var date = new Date(params.value[0]);"
			       +     "data = date.getFullYear() + '-'"
			       +            "+ (date.getMonth() + 1) + '-'"
			       +            "+ date.getDate() + ' '"
			       +            "+ date.getHours() + ':'"
			       +            "+ date.getMinutes();"
			       +     "return data + '<br/>'"
			       +            "+ params.value[1]" 
			       +            ";"
			       +"}");
		
		//工具箱  -------------------------------------
		EToolBox toolBox = lineCharts.createToolBox();
		//辅助箱工具
		toolBox.createMark();
		//数据视图工具,设置只读属性false
		toolBox.createDataView().setReadOnly(false);
		//刷新工具
		toolBox.createRestore();
		//图片保存工具
		toolBox.createSaveImage();
		
		//创建拖拽对象
		lineCharts.createCalculable();
		
		//数据缩放组件
		lineCharts.createDataZoom().setStart("0");
		
		//图例
		lineCharts.createLegend("动漫总数");
		
		//绘图区域
		lineCharts.createGrid().setY2("80");
		
		//X轴
		lineCharts.createXAxis().createAxisTime().setSplitNumber("30");
		
		//Y轴
		lineCharts.createYAxis().createAxisValue();
		
		//数据
		ESeries series = lineCharts.createSeries();
		addData(series);
		
		return lineCharts;
	}
	
	/**
	 * 数据填充
	 * @param series
	 */
	private static void addData(ESeries series) {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		//数据一
		ESeriesLine series1 = series.createESeriesLine();
		
		series1.setName("动漫总数");
		series1.setShowAllSymbol(true);
		series1.setSymbolSize("0");
		series1.setSmooth(true);
		
		DataTable dt = DBFactory.getActionDB().executeQueryTable("select * from LEO_CAN_REPORT_NUM order by RES_TIME");
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("(function(){ var d = [];");
		
		try {
			while(dt.next()){
				sb.append("d.push([new Date("+BusinessDateEx.parseDateLong(dt.getString("LEO_CAN_DATE_INF")+" 00:00:00", "yyyy-MM-dd HH:mm:ss")+"),"+dt.getString("LEO_CAN_NUM_TOTAL")+",1]);");				
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		sb.append("return d;})()");
		
		series1.createData().addImportData(sb.toString());
		
	}

}
